home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / boot / czesc_2 / tlpatch / source / tlpatch.c < prev    next >
C/C++ Source or Header  |  1991-05-09  |  6KB  |  258 lines

  1. /* TLPatch.c V1.0 */
  2. /* Translator.library patch utility */
  3. /* Richard Sheppard - Jan. 7, 1991 */
  4. /* Toronto, Canada */
  5.  
  6. #include <exec/types.h>
  7. #include <stdio.h>
  8. #include <exec/execbase.h>
  9. #include <exec/nodes.h>
  10.  
  11. char line[256];                                    /* line buffer */
  12. LONG codesize;                                        /* size of code & xtbl hunk */
  13. LONG jmptab[28];                                    /* jump table array */
  14. FILE *fp1, *fp2;                    /* input file (exception table), outfile */
  15. FILE *fp3;                            /* input file (translator.library) */
  16. WORD code[969];                                    /* original code at start */
  17. LONG reloc[38] =                                    /* reloc table at end of file */
  18. {
  19.     0x000003EC, 0x00000004,    0x00000000, 0x000000F4,
  20.     0x000000F0, 0x000000EC,    0x000000E8, 0x00000012,
  21.     0x00000000, 0x0000063E,    0x0000063A, 0x00000636,
  22.     0x00000632, 0x0000062E,    0x0000062A, 0x00000626,
  23.     0x00000622, 0x0000061E,    0x0000061A, 0x00000616,
  24.     0x00000612, 0x0000060E,    0x000005D6, 0x000003B2,
  25.     0x0000039A, 0x000001EE,    0x00000176, 0x00000001,
  26.     0x00000000, 0x000000F8,    0x00000001, 0x00000000,
  27.     0x000000C8, 0x00000000,    0x000003F2, 0x000003E9,
  28.     0x00000000, 0x000003F2
  29. };
  30. struct ExecBase *ExecBase = NULL;
  31. struct Node *node;
  32.  
  33. void main(),AddFile(),Validate(),Extract();
  34. int GetLine(char line[], int max);
  35. int GetExcept(char line[], int max);
  36. int StrIndex(char source[], char searchfor[]);
  37.  
  38. char pattern[] = "[A";                                    /* 1st search pattern */
  39.  
  40. void
  41. main(argc,argv)
  42. int argc;
  43. char *argv[];
  44. {
  45.     int c,opt,x = 0,xcnt = 0,size = 0;
  46.  
  47.     printf("\n\tTLPatch - translator.library patch utility V1.0\n");
  48.     printf("\t            Richard Sheppard - 1991\n\n");
  49.     while(--argc > 0)
  50.         if((*++argv)[0] == '-')                /* parse command line arguments */
  51.         {
  52.             while(opt = *++argv[0])
  53.                 switch(opt)
  54.                 {
  55.                     case 'x':
  56.                     case 'X':
  57.                         Extract();
  58.                         exit(0);
  59.                         break;
  60.                     default:
  61.                         break;
  62.                 }
  63.         }
  64.     if((fp1 = fopen("ram:except.tbl","r")) == NULL)
  65.     {
  66.         printf("*** Can't open ram:except.tbl\n");
  67.         printf("--- Copy your except.tbl to ram: or\n");
  68.         printf("--- use \"%s -x\" to extract table.\n",*argv);
  69.         exit(0);
  70.     }
  71.     else if((fp2 = fopen("ram:xtable.new","w")) == NULL)
  72.     {
  73.         printf("*** Can't open ram:xtable.new\n");
  74.         fclose(fp1);
  75.         exit(0);
  76.     }
  77.     else if((fp3 = fopen("sys:libs/translator.library","rb")) == NULL)
  78.     {
  79.         printf("*** Can't open sys:libs/translator.library\n");
  80.         fclose(fp2);
  81.         fclose(fp1);
  82.         exit(0);
  83.     }
  84.  
  85.     if(!(ExecBase = (struct ExecBase *)OpenLibrary("exec.library", 0L)))
  86.         printf("*** Can't open ExecBase\n");
  87.     else 
  88.         if((node = (struct Node *)FindName(&ExecBase->LibList,"translator.library")) != 0)
  89.         {
  90.             printf("\tRemoving resident translator.library\n");
  91.             Remove(node);
  92.         }
  93.  
  94.     printf("\tReading exception table ...\n");
  95.     while((c = GetLine(line,256)) > 0)
  96.     {
  97.         size = size + c;
  98.         if(StrIndex(line, pattern) >= 0)
  99.         {
  100.             jmptab[x] = (LONG)(size - c + 112);            /* 112 = table size */
  101.             x++;
  102.             pattern[1]++;                        /* increment search character */
  103.             if(pattern[1] == 91)
  104.                 pattern[1] = 48;                /* "0"  - start of numbers */
  105.             if(pattern[1] == 49)
  106.                 pattern[1] = 32;                /* " "  - start of punctuation */
  107.             if(pattern[1] == 33)
  108.                 pattern[0] = 0;                            /* NULL */
  109.         }
  110.         fprintf(fp2,"%s",line);                            /* write new xtable */
  111.         xcnt++;                                                /* exception count */
  112.     }
  113.     fputc('\0',fp2);                            /* NULL at end of exception table */
  114.     size++;                                                    /* size + NULL */
  115.     if((size + 2) % 4)
  116.     {
  117.         for(x = ((size + 2) % 4);x < 4;x++)
  118.         {
  119.             fputc('\0',fp2);                    /* align table to WORD boundary */
  120.             size++;
  121.         }
  122.     }
  123.     codesize = (size + 2014) / 4;                        /* 2014 = code + jmptab */
  124.     fclose(fp2);
  125.     fclose(fp1);
  126.     printf("\tReading original code ...\n");
  127.     Validate();
  128.     fclose(fp3);
  129.     if(codesize < 65536)
  130.     {
  131.         code[10] = 0x0000;                                /* patch hunk size */
  132.         code[11] = (WORD)codesize;
  133.     }
  134.     else
  135.     {
  136.         code[10] = (WORD)codesize / 65535;            /* patch hunk size */
  137.         code[11] = (WORD)codesize % 65535;
  138.     }
  139.     code[16] = code[10];                                    /* 2nd copy of hunk size */
  140.     code[17] = code[11];
  141.  
  142.     if((fp1 = fopen("ram:translator.library","wb")) == NULL)
  143.     {
  144.         printf("*** Can't create ram:translator.library\n");
  145.         exit(0);
  146.     }
  147.     fwrite((char *)&code,2,969,fp1);                    /* write 969 words */
  148.     fwrite((char *)&jmptab,2,56,fp1);                /* write new jump table */
  149.     AddFile();                                                /* append new xtable */
  150.     fwrite((char *)&reloc,2,76,fp1);                    /* write reloc table */
  151.     fclose(fp1);
  152.     remove("ram:xtable.new");
  153.     printf("\n\tThe new translator.library is now in ram:\n");
  154.     printf("\t\t\t%d exceptions\n\n",xcnt);
  155.     if(ExecBase)
  156.         CloseLibrary(ExecBase);
  157.     exit(0);
  158. }
  159.  
  160. int
  161. GetLine(char s[], int lim)
  162. {
  163.     int c, i;
  164.     i = 0;
  165.     while(--lim > 0 && (c=fgetc(fp1)) != EOF && c != '\n')
  166.         s[i++] = c;
  167.     s[i] = '\0';
  168.     return i;
  169. }
  170.  
  171. int
  172. GetExcept(char s[], int lim)
  173. {
  174.     int c, i;
  175.     i = 0;
  176.     while(--lim > 0 && (c=fgetc(fp3)) != EOF && c != '\\' && c != '`')
  177.         s[i++] = c;
  178.     if(c == '\\' || c == '`')
  179.         s[i++] = c;
  180.     s[i] = '\0';
  181.     return i;
  182. }
  183.  
  184. int
  185. StrIndex(char s[], char t[])
  186. {
  187.     int i;
  188.     for(i = 0;s[i] != '\0';i++)
  189.     {
  190.         if(s[i] == t[0] && s[i+1] == t[1])
  191.             return i;
  192.     }
  193.     return -1;
  194. }
  195.  
  196. void
  197. AddFile()
  198. {
  199.     int c;
  200.     if((fp2 = fopen("ram:xtable.new","r")) == NULL)
  201.     {
  202.         printf("*** Can't re-open ram:xtable.new\n");
  203.         fclose(fp1);
  204.         exit(0);
  205.     }
  206.     while((c = getc(fp2)) != EOF)
  207.         putc(c,fp1);
  208.     fclose(fp2);
  209.     return;
  210. }
  211.  
  212. void
  213. Validate()
  214. {
  215.     int x;
  216.     fread((char *)&code,2,969,fp3);                    /* read 969 words */
  217.     if(code[151] != 0x2033 || code[152] != 0x332E || code[153] != 0x3220)
  218.     {
  219.         printf("*** WARNING - not V33.2, unpredictable results could occur!\n");
  220.         for(x=0;x<3;x++)
  221.             printf("%04x ",code[151+x]);
  222.         printf("\n");
  223.     }
  224.     return;
  225. }
  226.  
  227. void
  228. Extract()
  229. {
  230.     int c;
  231.     printf("\tExtracting exception table ...\n");
  232.     if((fp1 = fopen("ram:except.tbl","w")) == NULL)
  233.     {
  234.         printf("*** Can't create ram:except.tbl\n");
  235.         exit(0);
  236.     }
  237.     else if((fp3 = fopen("sys:libs/translator.library","rb")) == NULL)
  238.     {
  239.         printf("*** Can't open sys:libs/translator.library\n");
  240.         fclose(fp1);
  241.         exit(0);
  242.     }
  243.     Validate();
  244.     fread((char *)&code,2,56,fp3);                    /* read jump table */
  245.     while((c = GetExcept(line,256)) > 0)
  246.     {
  247.         fprintf(fp1,"%s",line);
  248.         if(line[1] != '\\' && line[1] != '`')
  249.             fprintf(fp1,"\n");
  250.     }
  251.     fclose(fp3);
  252.     fclose(fp1);
  253.     printf("\n\tThe exception table (except.tbl) is now in ram:\n\n");
  254.     return;
  255. }
  256.  
  257. /* end of TLPatch.c */
  258.